package com.itic.mall.system.goods.propertyname.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.mall.system.inter.goods.propertyname.persistence.dao.PropertyNameDao;
import com.itic.mall.system.inter.goods.propertyname.persistence.model.PropertyName;
import com.itic.mall.system.inter.goods.propertyname.persistence.model.PropertyNameExample;
import com.itic.mall.system.inter.goods.propertyvalue.persistence.dao.PropertyValueDao;
import com.itic.mall.system.inter.goods.propertyvalue.persistence.model.PropertyValueExample;
import com.itic.mall.system.inter.goods.propertyvalue.persistence.model.PropertyValueExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class PropertyNameService
     extends CrudService<PropertyNameDao,PropertyName,PropertyNameExample> {
    @Autowired
    private PropertyNameDao propertyNameDao;
    
    @Autowired
    private PropertyValueDao propertyValueDao;
    
    /**
     * 判断属性名的个数
     * @author 王文鑫
     * @date 2018年1月21日 下午2:40:32
     * @param CategoryFirstId
     * @param CategorySecondId
     * @return
     */
    public int judgePropertyNameNum(String CategoryFirstId, String CategorySecondId){
    	return propertyNameDao.judgePropertyNameNum(CategoryFirstId,CategorySecondId);
    }
    
    
    /**
     * 级联删除
     * 
     * @param id
     * @return
     */
    public String deleteTree(String id) {
        int code = -1;
        PropertyName propertyName = new PropertyName();
        propertyName.setId(id);
        propertyName.setIsEffective(DBConst.TRUE);
        
		//先删除本级的ID
        PropertyName one = propertyNameDao.selectByPrimaryKey(id);
		
		if( null != one ){
			code += propertyNameDao.deleteByPrimaryKey(id);
			//删除下级
			PropertyValueExample rExample = new PropertyValueExample();// 逻辑删除资源信息
			Criteria criteria = rExample.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andNameIdEqualTo(one.getName());
			code += propertyValueDao.deleteByExample(rExample);
		}
        
        return 0 <= code ? SUCCESS : FAILED;
    }


}